//
//  XLNavigationVC.swift
//  XLWeiBo
//
//  Created by 李晓龙 on 16/9/19.
//  Copyright © 2016年 lixiaolong. All rights reserved.
//

import UIKit

class XLNavigationVC: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    //重写方法监听push的子视图控制器及导航视图控制器里面有几个子视图
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //获取导航控制器里面的子视图控制器
        print(viewControllers.count)
        //如果个数大于0表示不是根视图控制器
        if viewControllers.count > 0 {
          //表示第二个视图控制器
            if viewControllers.count == 1 {
            //获取上一级的title
            let title = viewControllers.first!.title!
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            
        }else{
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
        }
        
        viewController.title = "当前显示的是第\(viewControllers.count + 1)级控制器"
        }
        super.pushViewController(viewController, animated: true)
        //隐藏底部tabBar
//        viewController.hidesBottomBarWhenPushed = true
        
    }
    
    func backAction() {
        
        
        popViewController(animated: true)
    }
    
    //处理这次开始点击的手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        //如果是根视图的话 不处理边缘手势
        if viewControllers.count == 1 {
            return false
        }
        
        return true
    }
    
    
    
    
    
    
    
    
    
    
    

    

}
